package com.ziq.zcamp.main

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.ziq.base.base.BaseFragment
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.utils.LogUtil
import com.ziq.base.utils.SystemBarUtil
import com.ziq.zcamp.R
import com.ziq.zcamp.community.CommunityFragment
import com.ziq.zcamp.databinding.FragmentMainBinding
import com.ziq.zcamp.manager.PrivacyManager
import com.ziq.zcamp.mine.MineFragment
import com.ziq.zcamp.place.PlaceListFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlin.math.max

@AndroidEntryPoint
class MainFragment : BaseFragment(R.layout.fragment_main){

    private val binding : FragmentMainBinding? by binding()
    val mViewModel: MainFragmentViewModel by viewModels()

    private val fragments: MutableList<Pair<Int, Fragment>> = mutableListOf()

    enum class MainTab {
        MAIN_TAB_PLACE(10),
        MAIN_TAB_COMMUNITY(11),
        MAIN_TAB_MINE(12);

        var value:Int

        constructor(value: Int){
            this.value = value
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.root
        LogUtil.i(MainActivity.TAG, "main fragment onViewCreated ${childFragmentManager.fragments}")
        fragments.clear()

        addFragment(fragments, MainTab.MAIN_TAB_PLACE.value, PlaceListFragment::class.java)
        addFragment(fragments, MainTab.MAIN_TAB_COMMUNITY.value,CommunityFragment::class.java)
        addFragment(fragments, MainTab.MAIN_TAB_MINE.value, MineFragment::class.java)

        binding?.viewPager?.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return fragments[position].second
            }
        }

        binding?.viewPager?.isUserInputEnabled = false

        mViewModel.currentTabValue.observe(viewLifecycleOwner, Observer<Int> {tabValue ->
            val index = fragments.indexOfFirst { it.first == tabValue }
            binding?.viewPager?.setCurrentItem(index, false)
            updateViewForIndex()
        })

        mViewModel.currentTabValue.value = MainTab.MAIN_TAB_PLACE.value

        binding?.tabPlace?.click {
            if(mViewModel.currentTabValue.value != MainTab.MAIN_TAB_PLACE.value){
                PrivacyManager.checkPrivacy(requireActivity(), action = {
                    mViewModel.currentTabValue.value = MainTab.MAIN_TAB_PLACE.value
                })
            }
        }
        binding?.tabCommunity?.click {
            if(mViewModel.currentTabValue.value != MainTab.MAIN_TAB_COMMUNITY.value){
                PrivacyManager.checkPrivacy(requireActivity(), action = {
                    mViewModel.currentTabValue.value = MainTab.MAIN_TAB_COMMUNITY.value
                })
            }
        }
        binding?.tabMine?.click {
            if(mViewModel.currentTabValue.value != MainTab.MAIN_TAB_MINE.value){
                mViewModel.currentTabValue.value = MainTab.MAIN_TAB_MINE.value
            }
        }
        binding?.root?.let {
            SystemBarUtil.addSystemBarHeightChangeCallBack(it) { statusBarHeight: Int, navigationBarHeight: Int, keyboardHeight: Int ->
                binding?.root?.setPadding(0, 0, 0, max(navigationBarHeight, keyboardHeight))
            }
        }

    }

    private fun addFragment(fragments: MutableList<Pair<Int, Fragment>>, key: Int, cls : Class<*>){
        var fragment = childFragmentManager.fragments.firstOrNull { it::class.java.canonicalName == cls.canonicalName }
        if(fragment == null){
            fragment = cls.newInstance() as Fragment
        }
        fragments.add(Pair(key, fragment))
    }


    private fun updateViewForIndex(){
        val index = mViewModel.currentTabValue.value
        binding?.tabPlace?.isSelected = index == MainTab.MAIN_TAB_PLACE.value
        binding?.tabCommunity?.isSelected = index == MainTab.MAIN_TAB_COMMUNITY.value
        binding?.tabMine?.isSelected = index == MainTab.MAIN_TAB_MINE.value
    }


    fun switchTab(targetValue: Int?) {
        mViewModel.currentTabValue.value = targetValue
    }

}